<?php
/**
 * Web基础控制器
 * Created by PhpStorm.
 * User: yui
 * Date: 2017/4/21
 * Time: 12:05
 */

namespace common\controllers;

use Yii;

class WebCommonCtrl extends CommonCtrl
{
    /**
     * 用户token
     * @var
     */
    public $token;

    /**
     * 用户对象
     * @var
     */
    public $Member;

    public function init()
    {
        parent::init();
        $session = Yii::$app->session;
        $this->token = $session->get('token', '');
    }

    /**
     * 前置方法，获取用户对象信息
     * @param \yii\base\Action $action
     * @return bool
     */
    public function beforeAction($action)
    {
        $this->token = Yii::$app->request->get('token', '');
        if (!empty($this->token)) {
            $this->Member = Yii::$app->cache->get($this->token);
        }
        return parent::beforeAction($action);
    }

    /**
     * 后置方法，保存用户对象信息
     * @param \yii\base\Action $action
     * @param mixed $result
     * @return mixed
     */
    public function afterAction($action, $result)
    {
        if (!empty($this->Member)) {
            //保存信息
            Yii::$app->cache->set($this->Member->token, $this->Member, OC['session_lifetime']);
        }
        return parent::afterAction($action, $result);
    }

}